﻿$(document).ready(function () {
    //日历控件绑定默认日期，当天
    $(".txtInitialLoanDate").val(new Date().Format("yyyy/MM/dd"));

    var str = '<option value="-1">请选择</option>';
    var dept = new Dept();
    $("#filter a").click();
    //获取理财分部下拉列表
    $(".ddlBranch").change(function () {

        $(".ddlBusinessDep").html(str);
        $(".ddlTeam").html(str);
        $("#txtTeamManager").val("");
        $(".ddlCustomerManager").html(str);

        var branch = $(".ddlBranch").val();
        if (branch == -1) {
            $(".ddlFinaDep").html(str);
        }
        else {
            var finaDepList = dept.GetDeptDDLByPrentId(branch);
            $(".ddlFinaDep").html(finaDepList);
        }
        $(".ddlBusinessDep").html(str);
        $(".ddlTeam").html(str);
    });

    //获取营业部下拉列表
    $(".ddlFinaDep").change(function () {

        $(".ddlTeam").html(str);
        $("#txtTeamManager").val("");
        $(".ddlCustomerManager").html(str);
        var FinaDep = $(".ddlFinaDep").val();
        if (FinaDep != -1) {
            var BusinessDepList = dept.GetDeptDDLByPrentId(FinaDep);
            $(".ddlBusinessDep").html(BusinessDepList);
        }

    });


    //获取团队名称下拉列表
    $(".ddlBusinessDep").change(function () {

        var BusinessDep = $(".ddlBusinessDep").val();
        if (BusinessDep == -1) {
            $(".ddlTeam").html(str);
        }
        else {
            var TeamList = dept.GetDeptDDLByPrentId(BusinessDep);
            $(".ddlTeam").html(TeamList);
        }
    });

    //获取客户经理下拉列表
    $(".ddlTeam").change(function () {
        $("#txtTeamManager").val("");
        $(".ddlCustomerManager").html(str);
        var TeamId = $(".ddlTeam").val();
        if (TeamId != -1) {
            var teamData = dept.GetTeamMemberByTeamId(TeamId);
            $("#txtTeamManager").val(teamData.Manager.Name);
            var ddstr = '<option value="-1">请选择</option>';
            $(teamData.TeamList).each(function (i, e) {
                ddstr += '<option value="' + e.OptId + '">' + e.Name + '</option>';
            });
            $(".ddlCustomerManager").html(ddstr);
        }
    });

    //同步载入下拉列表信息
    $.ajax({
        type: "post",
        url: "../../../Handler/Operator.ashx",
        dataType: "json",
        data: { type: 14 },
        async: false,
        cache: true,
        complete: function () {
        },
        success: function (result) {

            if (result.Status) {
                if (result.Count == 0) {
                    //获取分公司下拉列表
                    var dept = new Dept();
                    var branchList = dept.GetDeptDDLByPrentId(2016); //2016是数据库中理财事业ID
                    $(".ddlBranch").html(branchList);
                }
                else {
                    $(result.Data.list).each(function (i, e) {
                        var select = "#searchArea select[id=select" + i + "]";
                        var opt = '<option value="' + e.DepId + '">' + e.DepName + '</option>';
                        $(select).html(opt);
                    });
                    if (result.Message == "CustomerManager") {
                        $("#txtTeamManager").val(result.Data.Manager);
                        var ddstr = '<option value="' + result.Data.OptId + '">' + result.Data.Name + '</option>';
                        $(".ddlCustomerManager").html(ddstr);
                        return;
                    }
                    else {
                        $("#searchArea select[id=select" + (result.Count - 1) + "]").trigger("change");
                        return;
                    }
                }
            }
        }
    });

    //筛选按钮点击后保存筛选信息
    $("#btnSearch").bind("click", function () {
        var serializer = new JSONSerializer();
        var $formData = serializer.Serialize($("#searchArea"));
        $formData.Branch = $(".ddlBranch").val() == -1 ? "" : $(".ddlBranch").find("option:selected").text();
        $formData.FinaDep = $(".ddlFinaDep").val() == -1 ? "" : $(".ddlFinaDep").find("option:selected").text();  // $(".ddlFinaDep option:selected").text();
        $formData.BusinessDep = $(".ddlBusinessDep").val() == -1 ? "" : $(".ddlBusinessDep").find("option:selected").text();  //$(".ddlBusinessDep option:selected").text();
        $formData.Team = $(".ddlTeam").val() == -1 ? "" : $(".ddlTeam").find("option:selected").text();   //$(".ddlTeam option:selected").text();
        $formData.CustomerManager = $(".ddlCustomerManager").val() == -1 ? "" : $(".ddlCustomerManager").find("option:selected").text();   //$(".ddlTeam option:selected").text();
        $formData.type = 0;
        $.ajax({
            type: "post",
            url: "../Handler/CustomInfoRepResultHandler.ashx",
            dataType: "json",
            async: false,
            data: $formData,
            success: function (data) {
                if (!data.Status) {
                    alert(data.Message);
                }
            }
        });
    });




});
var Dept = function () { };
Dept.prototype = {
    //根据父节点获取简单下拉列表
    GetDeptDDLByPrentId: function ($PrentId) {
        var result = "";
        $.ajax({
            type: "post",
            url: "../../../Handler/DeptHandler.ashx",
            dataType: "json",
            data: { type: 12, PrentId: $PrentId },
            async: false,
            cache: true,
            complete: function () {
            },
            success: function (data) {

                var str = '<option value="-1">请选择</option>';
                $(data).each(function (i, e) {
                    str += '<option value="' + e.DepId + '">' + e.DepName + '</option>';
                });
                result = str;
                console.log(result);
            }
        });
        return result;
    },
    GetTeamMemberByTeamId: function ($TeamId) {

        var teamManager;
        var result = "";
        $.ajax({
            type: "post",
            url: "../../../Handler/Operator.ashx",
            dataType: "json",
            data: { type: 13, teamId: $TeamId },
            async: false,
            cache: true,
            complete: function () {
            },
            success: function (data) {
                if (data.Status) {
                    result = data.Data;
                    console.log(result);
                }
                else {
                    alert(data.Message);
                }

            }
        });
        return result;
    }
}